import { Body, Controller, Get, Post, UseGuards } from '@nestjs/common';
import { ApiTags, ApiProperty } from '@nestjs/swagger';
import { AuthService } from './auth.service';
import { JwtAuthGuard } from './jwt.guard';
import { IsString, MinLength } from 'class-validator'

/**
 * 登录请求体 DTO
 */
class LoginDto {
  @ApiProperty()
  @IsString()
  @MinLength(3)
  username: string;

  @ApiProperty()
  @IsString()
  @MinLength(6)
  password: string;
}

/**
 * 认证控制器，处理登录和用户信息获取
 */ 
@ApiTags('auth')
@Controller('auth')
export class AuthController {
  constructor(private readonly authService: AuthService) {}

  /**
   * 登录路由，验证用户凭据并返回 JWT 令牌
   */
  @Post('login')
  async login(@Body() dto: LoginDto) {
    const user = await this.authService.validateUser(dto.username, dto.password);
    return this.authService.login(user);
  }

  /**
   * 受 JWT 认证保护的路由，返回用户个人信息
   */ 
  @UseGuards(JwtAuthGuard)
  @Get('profile')
  profile() {
    return { ok: true };
  }
}